/*
 * This file is part of nettool4j.
 * 
 * rockframework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * rockframework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>;.
 */
package net.woodstock.nettool4j.socket;

import net.woodstock.nettool4j.AbstractConsole;

public class TCPRedirectConsole extends AbstractConsole {

	// private static final String SOURCE_HOST_PARAMETER = "-sh";

	private static final String	SOURCE_PORT_PARAMETER	= "-sp";

	private static final String	DEST_HOST_PARAMETER		= "-dh";

	private static final String	DEST_PORT_PARAMETER		= "-dp";

	private static final String	SOCKET_HOST_PARAMETER	= "-ph";

	private static final String	SOCKET_PORT_PARAMETER	= "-pp";

	public TCPRedirectConsole(final String[] array) {
		super(array);
	}

	private boolean checkParameters() {
		if (this.getParameter(TCPRedirectConsole.SOURCE_PORT_PARAMETER) == null) {
			System.out.println("Parameter not found");
			System.out.println("\tparameter " + TCPRedirectConsole.SOURCE_PORT_PARAMETER + " must be defined");
			return false;
		}
		if (this.getParameter(TCPRedirectConsole.DEST_HOST_PARAMETER) == null) {
			System.out.println("Parameter not found");
			System.out.println("\tparameter " + TCPRedirectConsole.DEST_HOST_PARAMETER + " must be defined");
			return false;
		}
		if (this.getParameter(TCPRedirectConsole.DEST_PORT_PARAMETER) == null) {
			System.out.println("Parameter not found");
			System.out.println("\tparameter " + TCPRedirectConsole.DEST_PORT_PARAMETER + " must be defined");
			return false;
		}
		return true;
	}

	private void startRedirect() {
		if (this.checkParameters()) {
			int listen = Integer.parseInt(this.getParameter(TCPRedirectConsole.SOURCE_PORT_PARAMETER));
			String host = this.getParameter(TCPRedirectConsole.DEST_HOST_PARAMETER);
			int port = Integer.parseInt(this.getParameter(TCPRedirectConsole.DEST_PORT_PARAMETER));
			ProxyInfo proxyInfo = null;

			if (this.getParameter(TCPRedirectConsole.SOCKET_HOST_PARAMETER) != null) {
				if (this.getParameter(TCPRedirectConsole.SOCKET_PORT_PARAMETER) != null) {
					String proxyHost = this.getParameter(TCPRedirectConsole.SOCKET_HOST_PARAMETER);
					int proxyPort = Integer.parseInt(this.getParameter(TCPRedirectConsole.SOCKET_PORT_PARAMETER));
					proxyInfo = new ProxyInfo(proxyHost, proxyPort);
				}
			}

			RedirectInfo info = new RedirectInfo(listen, host, port);
			TCPRedirect redirect = new TCPRedirect(info, proxyInfo);
			Thread thread = new Thread(redirect);
			thread.start();
		}
	}

	public static void main(final String[] args) {
		TCPRedirectConsole console = new TCPRedirectConsole(args);
		console.startRedirect();
	}

}
